package com.wesly.plugindemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.wesly.plugindemo.hook.ReflectUtil;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Environment;
import android.provider.SyncStateContract;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.io.File;

import top.vimerzhao.plugin.Constants;
import top.vimerzhao.plugin.PluginManager;
import top.vimerzhao.plugin.VLog;

public class MainActivity extends Activity {
    public final static String PLUGIN_NAME = "plugin.apk";
    public final static String PLUGIN_PACKAGE_NAME = "top.vimerzhao.imageloader";
    public final static String PLUGIN_CLAZZ_NAME = "top.vimerzhao.imageloader.MainActivity";

    private String mPluginPackageName = "top.vimerzhao.image";
    private String mPluginClassName = "top.vimerzhao.image.MainActivity";

    PluginManager mPluginManager;

    com.wesly.plugindemo.hook.PluginManager hookPluginManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.tv_permission).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Utils.verifyStoragePermissions(MainActivity.this);
            }
        });

        findViewById(R.id.tv_content).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra(Constants.PACKAGE_NAME, PLUGIN_PACKAGE_NAME);
                intent.putExtra(Constants.PLUGIN_CLASS_NAME, PLUGIN_CLAZZ_NAME);
                mPluginManager.startActivity(intent);
            }
        });

        findViewById(R.id.tv_content2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (hookPluginManager.loadPlugin(com.wesly.plugindemo.hook.Constants.PLUGIN_PATH)) {
                    Intent intent1 = new Intent();
                    intent1.setClassName(mPluginPackageName, mPluginClassName);
//                    intent.setClassName(PLUGIN_PACKAGE_NAME, PLUGIN_CLAZZ_NAME);
                    startActivity(intent1);


//                    Intent intent = new Intent(MainActivity.this, StubActivity.class);
//                    Bundle bundle = new Bundle();
//                    intent.putExtras(bundle);
//                    MainActivity.this.startActivity(intent);
                }
            }
        });

        Utils.verifyStoragePermissions(this);

        PluginManager.init(getApplicationContext());
        mPluginManager = PluginManager.getInstance();
        String pluginApkPath = Environment.getExternalStorageDirectory() +
                File.separator + "plugins" + File.separator + PLUGIN_NAME;


        VLog.log("can read: " + Environment.getExternalStorageDirectory().canRead());
        VLog.log(pluginApkPath);
        mPluginManager.loadApk(pluginApkPath);

        initHookPlugin();

        TextView tv1 = findViewById(R.id.tv_plugin1);
        tv1.setText(pluginApkPath);

        TextView tv2 = findViewById(R.id.tv_plugin2);
        tv2.setText(com.wesly.plugindemo.hook.Constants.PLUGIN_PATH);
    }

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        // !!! 不要在此Hook,看源码发现mInstrumentaion会在此方法后初始化
    }

    private void initHookPlugin() {
        // !! must first
        ReflectUtil.init();

        hookPluginManager = com.wesly.plugindemo.hook.PluginManager.getInstance(getApplicationContext());
        hookPluginManager.hookInstrumentation();
        hookPluginManager.hookCurrentActivityInstrumentation(this);
    }
}